package com.naiterui.ehp.bs.cms.modules.agent.controller;

import com.naiterui.ehp.bp.constants.SwaggerTag;
import com.naiterui.ehp.bp.security.annotation.SysLog;
import com.naiterui.ehp.bp.security.utils.SecurityUtils;
import com.naiterui.ehp.bs.cms.common.utils.JwtUser;
import com.naiterui.ehp.bs.cms.common.vo.PageParamsVO;
import com.naiterui.ehp.bs.cms.common.vo.PageVO;
import com.naiterui.ehp.bs.cms.modules.agent.entity.AgNoticeMessage;
import com.naiterui.ehp.bs.cms.modules.agent.po.NoticeEditPO;
import com.naiterui.ehp.bs.cms.modules.agent.po.NoticePagePO;
import com.naiterui.ehp.bs.cms.modules.agent.service.IAgentNoticeService;
import com.naiterui.ehp.bs.cms.modules.agent.vo.NoticeItemVO;
import com.naiterui.ehp.bs.cms.modules.agent.vo.NoticePageItemVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author yiren
 * @since 2022/2/10
 */
@Slf4j
@Api(tags = {SwaggerTag.CMS + SwaggerTag.AGENT_MANGEN})
@AllArgsConstructor
@RestController
@RequestMapping("/agent/notice")
@PreAuthorize("@p.check('agent:notice')")
public class AgentNoticeController {
	private final IAgentNoticeService agentNoticeService;

	@SysLog("查询消息列表")
	@GetMapping("/list")
	@ApiOperation("查询消息列表")
	// @PreAuthorize("@p.check('agent:notice:list')")
	public PageVO<NoticePageItemVO> list(PageParamsVO pageParamsVO, NoticePagePO pagePO) {

		return agentNoticeService.queryPage(pageParamsVO, pagePO);
	}

	@SysLog("查询消息详情")
	@PostMapping("/get")
	@ApiOperation("查询消息详情")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "id", value = "消息ID", dataType = "long", paramType = "query", required = true),
	})
	// @PreAuthorize("@p.check('agent:notice:get')")
	public NoticeItemVO get(Long id) {

		return agentNoticeService.getDetail(id);
	}

	@SysLog("编辑/新增保存消息")
	@PostMapping("/save")
	@ApiOperation("编辑/新增保存消息")
	// @PreAuthorize("@p.check('agent:notice:save')")
	public void save(NoticeEditPO editPO) {
		JwtUser jwtUser = SecurityUtils.getCurrentUser();
		agentNoticeService.saveNotice(editPO,jwtUser.getUsername());
	}

	@SysLog("消息发布")
	@PostMapping("/release")
	@ApiOperation("消息发布")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "id", value = "消息ID", dataType = "long", paramType = "query", required = true),
	})
	// @PreAuthorize("@p.check('agent:notice:release')")
	public void release(Long id) {
		JwtUser jwtUser = SecurityUtils.getCurrentUser();
		agentNoticeService.updateNoticeStatus(id, AgNoticeMessage.STATUS_RELEASE, jwtUser.getUsername());
	}

    @SysLog("取消消息发布")
	@PostMapping("/release/cancel")
    @ApiOperation("取消消息发布")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "id", value = "消息ID", dataType = "long", paramType = "query", required = true),
	})
	// @PreAuthorize("@p.check('agent:notice:release:cancel')")
	public void cancelRelease(Long id) {
		agentNoticeService.updateNoticeStatus(id, AgNoticeMessage.STATUS_UNRELEASE, "");
	}
}
